/**
 * Created by litengfei on 2017/3/27.
 * Update by gh on 2018/3/28.
 * 内存检测机制
 */
var memwatch = require('memwatch-next');
var heapdump = require('heapdump');
class MemoryLeakManager{
    constructor(){

    }

    static writeMemory(path){
        heapdump.writeSnapshot(path +"//"+ Date.now() + '.heapsnapshot');
    }

    static autoWatchMemory(path){//自动监测内存溢出，然后将泄漏信息写在固定的文件里
        memwatch.on('leak', function(info) {
            console.error(info);
            var file = path + "//" + process.pid + '-' + Date.now() + '.heapsnapshot';
            heapdump.writeSnapshot(file, function(err){
                if (err) console.error(err);
                else console.error('Wrote snapshot: ' + file);
            });
            //heapdump.writeSnapshot(path +"//"+ Date.now() + '.heapsnapshot');
        });

    }
}
module.exports = MemoryLeakManager;
